package com.jomoo.css.common.util.service;

import com.jomoo.css.common.util.client.SYSUAAFeignClient;
import com.jomoo.css.common.util.errors.BadRequestAlertException;
import com.jomoo.css.common.util.security.AuthenticationUser;
import com.jomoo.css.common.util.security.AuthorizationLogin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 实体[SYSUSER] 服务对象接口实现
 */
@Service("SYSUAAUserService")
public class SYSUAAUserService implements AuthenticationUserService {

    @Autowired
    private SYSUAAFeignClient uaaFeignClient;

    @Override
    public AuthenticationUser loadUserByUsername(String username) {
        AuthenticationUser user = uaaFeignClient.loginByUsername(username);
        if (user == null) {
            throw new BadRequestAlertException("登录失败", "SYSUAAUser", username);
        }
        return user;
    }

    @Override
    public AuthenticationUser loadUserByLogin(String username, String password) {
        String[] data = username.split("[|]");
        String loginname = username;
        String domains = "";

        if (data.length == 2) {
            loginname = data[0].trim();
            domains = data[1].trim();
        }
        AuthorizationLogin logininfo = new AuthorizationLogin();
        logininfo.setDomain(domains);
        logininfo.setLoginname(loginname);
        logininfo.setPassword(password);
        AuthenticationUser user = uaaFeignClient.login(logininfo);
        if (user == null) {
            throw new BadRequestAlertException("登录失败", "SYSUAAUser", username);
        }
        return user;
    }


    @Override
    public void resetByUsername(String username) {

    }

    @Override
    public AuthenticationUser loadUserByTelephone(String telephone, String captcha, String userCode) {
        return null;
    }

    @Override
    public List<AuthenticationUser> loadUserByTelephones(String telephone, String captcha) {
        return null;
    }

    @Override
    public AuthenticationUser loadUserByCode1(String code) {
        return null;
    }

}
